<?php

namespace app\shop\controller\welfare;

use app\common\model\store\Store;
use app\shop\controller\Controller;
use app\shop\model\welfare\Officer as OfficerModel;

class Officer extends Controller
{

    public function index()
    {
        $model = new OfficerModel();
        $list = $model->getList($this->postData());

        $store_list = Store::where('is_delete',0) -> field('store_name,shop_supplier_id') -> select();
        $store_array = [];
        foreach ($store_list as $v){
            $store_array[$v['shop_supplier_id']][] = $v['store_name'];
        }
        foreach ($list as $key => $value){
            $list[$key]['store_name'] = $store_array[$value['shop_supplier_id']]??'';
        }

        return $this->renderSuccess('', compact('list'));

    }

    public function audit($welfare_officer_id, $audit, $reject_reason="")
    {
        $model = OfficerModel::detail($welfare_officer_id);
        if ($model["is_audit"] != 0) {
            return $this->renderError('请勿重复操作');
        }
        if (!in_array($audit, [1, 2])) {
            return $this->renderError('非法操作');
        }
        if ($audit == 2 && empty(trim($reject_reason))) {
            return $this->renderError('请填写不通过原因');
        }
        $model->save(['is_audit' => $audit, 'reject_reason' => $audit == 2 ? trim($reject_reason) : $model['reject_reason']]);
        return $this->renderSuccess('', '操作成功');
    }

    public function delete($welfare_officer_id)
    {
        $model = new OfficerModel;
        $model->where("welfare_officer_id",$welfare_officer_id)->save(["is_delete"=>1]);
        return $this->renderSuccess('操作成功');
    }

    /**
     *
     * edit
     * @param $welfare_officer_id
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/5/22 14:32
     */
    public function edit($welfare_officer_id)
    {
        $model = OfficerModel::detail($welfare_officer_id, ['bg_image', 'qr_code']);
        if ($this->request->isGet()) {
            return $this->renderSuccess('更新成功', compact('model'));
        }
        $data = $this->postData();
        // 新增记录
        if ($model->edit($data)) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

}